package problema055;

import java.math.BigInteger;

public class LychrelNumbers {
	
	static BigInteger diez = new BigInteger("10");
	
	public static void main(String[] args){
		System.out.println(cantLychrel());
	}
	
	public static int cantLychrel(){
		int res = 0;
		for(int i = 0; i < 10000; i++){
			if(esLychrel(i))res++;
		}		
		return res;
	}
	
	public static boolean esLychrel(int n1){
		BigInteger n = new BigInteger(Integer.toString(n1));
		for(int i = 0; i < 50; i++){
			n = n.add(revertir(n));
			if(esPalindromo(n)) return false;
		}
		return true;
	}
	
	public static boolean esPalindromo(BigInteger num){
		BigInteger reverso = BigInteger.ZERO, act = BigInteger.ZERO, orig = num;
		while (!num.equals(BigInteger.ZERO)) {
		      act = num.mod(diez);
		      reverso = reverso.multiply(diez).add(act);
		      num = num.divide(diez);
		 }
		return orig.equals(reverso);
	}
	
	public static BigInteger revertir(BigInteger num){
		BigInteger reverso = BigInteger.ZERO, act = BigInteger.ZERO;
		while (!num.equals(BigInteger.ZERO)) {
		      act = num.mod(diez);
		      reverso = reverso.multiply(diez).add(act);
		      num = num.divide(diez);
		 }
		return reverso;
	}
	

	
}
